package avicit.bdp.dms.prm.rest;

import avicit.bdp.common.service.dto.ConfigDTO;
import avicit.bdp.common.utils.ConfigUtils;
import avicit.bdp.common.utils.redis.RedisCacheHelper;
import avicit.bdp.dms.prm.service.ConfigService;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * 配置信息REST接口
 */

@RestController
@Api(tags = "配置信息REST接口")
@RequestMapping("/api/bdp/dms/prm/config")
public class ConfigRest {

    @Autowired
    private ConfigService configService;
    @Autowired
    private RedisCacheHelper redisCacheHelper;

    /**
     * 查询配置信息
     */
    @ApiOperation(value = "查询配置")
    @GetMapping("/getConfigs")
    public ResponseMsg<List<ConfigDTO>> getConfigs() {
        ResponseMsg<List<ConfigDTO>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(configService.getConfigList());
        return responseMsg;
    }

    /**
     * 查询配置信息
     */
    @ApiOperation(value = "查询配置")
    @PostMapping("/findConfigList")
    public ResponseMsg<List<ConfigDTO>> findConfigList(@RequestBody ConfigDTO config) {
        ResponseMsg<List<ConfigDTO>> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(configService.findConfigList(config));
        return responseMsg;
    }

    /**
     * 修改配置信息
     */
    @ApiOperation(value = "修改配置")
    @PostMapping("/updateConfig")
    public ResponseMsg<Boolean> updateConfig(@RequestBody ConfigDTO config) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(configService.updateConfig(config));
        return responseMsg;
    }

    /**
     * 清空配置信息缓存
     */
    @ApiOperation(value = "清空缓存")
    @PostMapping("/clearConfig")
    public ResponseMsg<Boolean> clearConfig() {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        Set<String> keys = redisCacheHelper.keys("bdp:*");
        for(String key: keys){
            redisCacheHelper.del(key);
        }
        ConfigUtils.getInstance().reset();
        return responseMsg;
    }
}
